<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.07.28.
   家居商家在建工地控制器($$)*/

!defined('Q_PATH') && exit;

class JiajubuildController extends AController{

	public function filter_(&$arrMap){
		$arrMap['A.jiajubuild_title']=array('like',"%".Q::G('jiajubuild_title')."%");
		$arrMap['A.jiajubuild_owner']=array('like',"%".Q::G('jiajubuild_owner')."%");
		$arrMap['A.jiajubuild_viewnum']=array('egt',intval(Q::G('jiajubuild_viewnum')));
		$arrMap['A.jiajubuild_realprice']=array('egt',intval(Q::G('jiajucase_realprice')));
		$arrMap['A.jiajubuild_realspace']=array('egt',intval(Q::G('jiajucase_realspace')));
		
		// 添加时间
		$this->getTime_('A.create_dateline',$arrMap);

		// 待审
		$this->getAudit_('A.jiajubuild_status',$arrMap);
	}

	public function bAdd_(){
		// 工地属性
		$oJiajucompanyclass=Q::instance('JiajucompanyclassModel');
		$this->assign('arrStyleclass',$oJiajucompanyclass->getClass(52));
		$this->assign('arrSpaceclass',$oJiajucompanyclass->getClass(53));
		$this->assign('arrPriceclass',$oJiajucompanyclass->getClass(50));
		$this->assign('arrAreaclass',$oJiajucompanyclass->getClass(51));
	}

	public function index($sModel=null,$bDisplay=true){
		parent::index('jiajubuild',false);
		$this->display(Admin_Extend::template('jiaju','jiajubuild/index'));
	}

	protected function sqljoin_(){
		return "->join('".Q::C('DB_PREFIX')."jiajucompany AS C','C.jiajucompany_id,C.jiajucompany_fullname,C.jiajucompany_isfvip,C.jiajucompany_domain','A.jiajucompany_id=C.jiajucompany_id')";
	}

	public function bEdit_(){
		$this->bAdd_();
	}

	public function add(){
		$this->bAdd_();
		$this->display(Admin_Extend::template('jiaju','jiajubuild/add'));
	}

	public function get_designer(){
		$nCpId=intval(Q::G('cp_id','G'));
		$nDesignerId=intval(Q::G('designer_id','G'));
		if(!$nCpId){
			exit('<span style="color:red;">你没有选择任何商家</span>');
		}

		$arrDesigners=Model::F_('jiajudesigner')->where(array('jiajucompany_id'=>$nCpId,'jiajudesigner_status'=>1))
			->setColumns('jiajudesigner_id,jiajudesigner_name')
			->order('jiajudesigner_id DESC')
			->getAll();

		echo '<select name="jiajudesigner_id" id="jiajudesigner_id" class="">
				<option value="">- 请选择 -</option>';
		foreach($arrDesigners as $arrDesigner){
			echo "<option value=\"{$arrDesigner['jiajudesigner_id']}\" ".($nDesignerId==$arrDesigner['jiajudesigner_id']?'selected':'').">{$arrDesigner['jiajudesigner_name']}</option>";
		}
		echo '</select>';
	}

	public function edit($sMode=null,$nId=null,$bDidplay=true){
		$nId=intval(Q::G('value','G'));
		parent::edit('jiajubuild',$nId,false);
		$this->bEdit_();
		$this->display(Admin_Extend::template('jiaju','jiajubuild/add'));
	}

	public function insert($sModel=null,$nId=null){
		$nId=Q::G('value');
		parent::insert('jiajubuild',$nId);
	}

	public function update($sModel=null,$nId=null){
		$nId=Q::G('value');
		parent::update('jiajubuild',$nId);
	}

	public function forbid($sModel=null,$sId=null,$bApp=false){
		$nId=intval(Q::G('value','G'));
		parent::forbid('jiajubuild',$nId,true);
	}

	public function resume($sModel=null,$sId=null,$bApp=false){
		$nId=intval(Q::G('value','G'));
		parent::resume('jiajubuild',$nId,true);
	}

	public function clear_recycle($sModel=null,$sField='status'){
		parent::clear_recycle('jiajubuild',$sField);
	}

	public function foreverdelete_deep($sModel=null,$sId=null){
		$sId=Q::G('value');
		parent::foreverdelete_deep('jiajubuild',$sId);
	}

	public function foreverdelete($sModel=null,$sId=null,$bApp=false){
		$sId=Q::G('value');
		parent::foreverdelete('jiajubuild',$sId,true);
	}

	public function diary(){
		$nId=intval(Q::G('value','G'));
		if(empty($nId)){
			$this->E('未指定在建工地');
		}

		$arrBuild=Model::F_('jiajubuild','jiajubuild_id=?',$nId)
			->setColumns('jiajubuild_id,jiajubuild_title')
			->getOne();
		if(empty($arrBuild['jiajubuild_id'])){
			$this->E('该在建工地不存在');
		}

		$arrDiarys=Model::F_('jiajubuilddiary','jiajubuild_id=?',$nId)
			->order('jiajubuilddiary_progress DESC')
			->getAll();

		$this->assign('arrDiarys',$arrDiarys);
		$this->assign('arrBuild',$arrBuild);
		$this->display(Admin_Extend::template('jiaju','jiajubuild/diary'));
	}

	public function adddiary(){
		$nBid=intval(Q::G('bid'));
		if(empty($nBid)){
			$this->E('未指定在建工地');
		}

		$arrBuild=Model::F_('jiajubuild','jiajubuild_id=?',$nBid)
			->getOne();
		if(empty($arrBuild['jiajubuild_id'])){
			$this->E('该在建工地不存在');
		}

		if($arrBuild['jiajubuild_progress']==7){
			$this->E('该在建工地已经施工结束，无法再添加工程进度。');
		}

		// 保存
		if($this->isPost()){
			$oDiary=new JiajubuilddiaryModel();
			$oDiary->jiajubuild_id=$nBid;
			$oDiary->jiajucompany_id=$arrBuild['jiajucompany_id'];
			$oDiary->save();
			if($oDiary->isError()){
				$this->E($oDiary->getErrorMessage());
			}else{
				Model::M_('jiajubuild')->updateWhere(array('jiajubuild_progress'=>intval(Q::G('jiajubuilddiary_progress'))),'jiajubuild_id=?',$nBid);
				
				$this->A($oDiary->toArray(),'施工记录添加成功',1);
			}
		}

		$this->assign('arrBuild',$arrBuild);
		$this->assign('nBid',$nBid);
		$this->display(Admin_Extend::template('jiaju','jiajubuild/adddiary'));
	}

	public function editdiary(){
		$nBid=intval(Q::G('bid'));
		$nId=intval(Q::G('value'));
		if(empty($nBid)){
			$this->E('未指定在建工地');
		}

		$arrBuild=Model::F_('jiajubuild','jiajubuild_id=?',$nBid)
			->getOne();
		if(empty($arrBuild['jiajubuild_id'])){
			$this->E('该在建工地不存在');
		}

		$oDiary=JiajubuilddiaryModel::F('jiajubuild_id=? AND jiajubuilddiary_id=?',$nBid,$nId)->getOne();
		if(empty($oDiary['jiajubuilddiary_id'])){
			$this->E('该施工记录不存在');
		}

		// 保存
		if($this->isPost()){
			$oDiary->save('update');
			if($oDiary->isError()){
				$this->E($oDiary->getErrorMessage());
			}else{
				$this->S('施工记录更新成功');
			}
		}

		$this->assign('arrBuild',$arrBuild);
		$this->assign('oValue',$oDiary);
		$this->assign('nBid',$nBid);
		$this->assign('nId',$nId);
		$this->display(Admin_Extend::template('jiaju','jiajubuild/adddiary'));
	}

	public function order(){
		$nId=intval(Q::G('value','G'));
		if(empty($nId)){
			$this->E('未指定在建工地');
		}

		$arrBuild=Model::F_('jiajubuild','jiajubuild_id=?',$nId)
			->setColumns('jiajubuild_id,jiajubuild_title')
			->getOne();
		if(empty($arrBuild['jiajubuild_id'])){
			$this->E('该在建工地不存在');
		}

		$arrOrders=Model::F_('jiajubuildorder','jiajubuild_id=? AND jiajubuildorder_status != 9',$nId)
			->order('jiajubuildorder_id DESC')
			->getAll();

		$this->assign('arrOrders',$arrOrders);
		$this->assign('arrBuild',$arrBuild);
		$this->display(Admin_Extend::template('jiaju','jiajubuild/order'));
	}

	public function set_status(){
		$nId=intval(Q::G('value'));
		$nStatus=intval(Q::G('status'));
		if(empty($nId)){
			$this->E('未指定预约参观订单');
		}
		if(!in_array($nStatus,array(1,13,9))){
			$this->E('预约参观订单状态值错误');
		}
		
		$oOrder=JiajubuildorderModel::F('jiajubuildorder_id=?',$nId)->getOne();
		if(empty($oOrder)){
			$this->E('预约参观订单不存在');
		}

		$oOrder->jiajubuildorder_status=$nStatus;
		$oOrder->save('update');
		if($oOrder->isError()){
			$this->E($oOrder->getErrorMessage());
		}else{
			$this->S('操作成功');
		}
	}

	public function orderStatus($status){
		switch($status){
			case 0:// 客户下单
				return '<span style="color:blue;">等待确认</span>';
				break;
			case 1:// 商家确认
				return '<span style="color:#0DD07F;">商家已确认</span>';
				break;
			case 13:
				return '<span style="color:gray;">订单关闭</span>';
				break;
			case 9:
				return '<span style="color:red;">回收站</span>';
				break;
			default:
				return '未知';
				break;
		}
	}

	public function view_preview($sMode=null,$nId=null,$bDidplay=true){
		$nId=intval(Q::G('value','G'));
		parent::view_preview('jiajubuild',$nId,false);
		$this->display(Admin_Extend::template('jiaju','jiajubuild/viewpreview'));
	}

	public function save_preview($sMode=null,$nId=null){
		$nId=intval(Q::G('value','G'));
		parent::save_preview('jiajubuild',$nId);
	}

	public function save_previewstatus($sMode=null,$nId=null){
		$nId=intval(Q::G('value','G'));
		parent::save_previewstatus('jiajubuild',$nId);
	}

}
